$(function() {
	admin.user.init();
});
Namespace('admin.user',{
	init:function() {
		easyui.util.initDatagrid('#datagrid',{
			onDblClickRow:function() {
				//admin.user.update();
			}
		});
	},
	query:function() {
		if(!$('#query-form').form('validate')) return;
		var data=$('#query-form').form('jsonObject');
		$('#datagrid').datagrid('load',data);
	},
	detail:function(id) {
		if (id != null) {
			top.addTab(id,'后台用户详情', contextPath+'/sys/user/detail/' + id);
		}
	},
	update:function(id) {
		top.addTab(id,'后台用户修改', contextPath+'/sys/user/update/' + id);
	},
	doUpdate:function() {
		if(!$('#data-form').form('validate')) return;
		var data = $('#data-form').form('jsonObject');
		var err = [];
		if(String.isEmpty(data.id)) {
			top.showInfo('系统异常，请稍后再试！');
			return;
		}
		if(String.isEmpty(data.realName)) {
			err.push('姓名不能为空');
		}
		if(String.isEmpty(data.birthdayStr)) {
			err.push('出生日期不能为空');
		}
		if(String.isEmpty(data.graduated)) {
			err.push('毕业院校不能为空');
		}
		if(String.isEmpty(data.htNextAddr)) {
			err.push('家乡地址不能为空');
		}
		var intersts = [];
		$("input.check_txt:checked").each(function(){
			intersts.push(this.value);
		});
		if (intersts.length == 0) {
			err.push('兴趣爱好不能为空');
		}
		data.intersts = intersts.join(',');
		var skill = document.getElementsByName("skill");
		var skills = [];
		for (var i=0;i<skill.length;i++) {
			if(skill[i].value){
				skills.push(skill[i].value);
			}
		}
		if (skills.length == 0) {
			err.push('职业技能不能为空');
		}
		data.skills = skills.join(',');
		delete data.skill;
		if(err&&err.length>0){
			top.showInfo(err.join('\n'));
			return;
		}
		data.htProvinceId = document.getElementById("htProvinceId").value,
		data.htCityId = document.getElementById("htCityId").value,
		data.htRegionId = document.getElementById("htRegionId").value,
		$.postJson(contextPath+'/admin/user/update/'+data.id,data,function(result) {
			//$('#datagrid').datagrid('loaded');
			if(result.code==0) {
				top.showInfo(result.msg);
				admin.user.detail(data.id);
				//$('#datagrid').datagrid('reload');
			} else {
				top.showInfo(result.msg);
			}
		},function(result,textstatus) {
			top.showInfo(result.responseText);
		});
	},
	add:function() {
		top.addTab("1",'新增用户', '/user/user-add.jsp');
	},
	doAdd:function() {
		
	},
	del:function(id) {
		  $.messager.confirm('信息提示', "确定要除名？", function (success) {
	            if (!success) {
	                return;
	            }
	            $('#datagrid').datagrid('loading');
	            $.post(contextPath+'/admin/user/del', {'id': id}, function (result) {
	                $('#datagrid').datagrid('loaded');
	                if (result.code == 0) {
	                    top.showInfo('操作成功');
	                    admin.user.query();
	                } else {
	                    top.showInfo(result.msg);
	                }
	            });

	        });
	},
	toRevokePhone:function(id) {
		top.addTab(id,'注销手机号', contextPath+'/admin/user/revokePhone');
	},

	revokePhone:function() {
		$('#operTip').html('');
		if (!$('#data-form').form('validate')) return;
		var data = $('#data-form').form('jsonObject');
		var err = [];
		if (String.isEmpty(data.mobile)) {
			top.showInfo('手机号不能为空');
			return;
		}
		$.post(contextPath + '/admin/user/revokePhone', data, function (result) {
			if (result.code == 0) {
				$('#mobile').val('');
				$('#operTip').html('手机号'+data.mobile+'注销成功');
				top.showInfo('手机号注销成功');
				return;
			}
			var errorMsg = '手机号注销失败!' + result.msg;
			$('#operTip').html(errorMsg);
			top.showInfo(errorMsg);
		});
	},
	addAdminUser:function() {
		getDialog('add-dialog').dialog({
			href:contextPath+'/user/admin/add',
			title: '添加用户',
		    width: 500,
		    height: 320,
		    buttons:[{
				text:'确定',
				iconCls:'icon-ok',
				handler:function() {
					admin.user.doAddAdminUser();
				}
			},{
				text:'取消',
				iconCls:'icon-cancel',
				handler:function(){
					$('#add-dialog').dialog('close');
				}
			}]
		});
	},
	doAddAdminUser:function() {
		if(!$('#add-form').form('validate')) return;
		var data = $('#add-form').form('jsonObject');
		$('#add-dialog').dialog('close');
		$('#datagrid').datagrid('loading');
		$.post(contextPath + '/user/admin/add',data,function(result) {
			$('#datagrid').datagrid('loaded');
			if(result.code==1) {
				top.showInfo('添加成功!');
				$('#datagrid').datagrid('reload');
			} else {
				top.showInfo('添加失败!');
			}
		});
	},
	addRole:function(userId) {
		getDialog('add-dialog').dialog({
			href:contextPath+'/user/admin/addRole/'+userId,
			title: '分配角色',
		    width: 500,
		    height: 320,
		    buttons:[{
				text:'确定',
				iconCls:'icon-ok',
				handler:function() {
					admin.user.doAddRole();
				}
			},{
				text:'取消',
				iconCls:'icon-cancel',
				handler:function(){
					$('#add-dialog').dialog('close');
				}
			}]
		});
	},
	doAddRole:function() {
		var row = $('#datagrid').datagrid('getSelected');
		var data = {id : row.id};
		$('#add-dialog').dialog('close');
		
		$('#datagrid').datagrid('loading');
		admin.user.setCheckedRole(data,'#add-role-tree');
		$.post(contextPath+'/user/admin/addRole/'+row.id,data,function(result) {
			$('#datagrid').datagrid('loaded');
			if(result.code==1) {
				top.showInfo('修改成功!');
				$('#datagrid').datagrid('reload');
			} else {
				top.showInfo('修改失败!');
			}
		});
	},
	selectTab:function(title,roletree) {
		if(title!='分配角色') return;
		if($(roletree).data('loaded')) return;	//如果已经加载了角色树,那么无需再加载
	
		$(roletree).tree({
			checkbox: true,
			animate:true,
			onClick:function(node){
				if($(node.target).find('.tree-checkbox0').size()>0) {
					$(this).tree('check', node.target);
				} else {
					$(this).tree('uncheck', node.target);
				}
			},
			onLoadSuccess:function() {
				$(roletree).data('loaded',true);
			}
		});
	},
	/**
	 * 设置已选择的角色
	 */
	setCheckedRole:function(user,jqtree) {
		if($(jqtree).data('loaded')) {
			var checkedRoleId=[];
			var checkedNodes=$(jqtree).tree('getChecked');
			for(var i in checkedNodes) {
				checkedRoleId.push(checkedNodes[i].id);
			}
			console.info('******************:'+checkedRoleId);
			user.roleIds=checkedRoleId.join(',');
		}
	}
});